package com.wuliu.system.service.impl;

import java.util.*;

import cn.hutool.core.util.IdUtil;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.DictUtils;
import com.wuliu.system.domain.SysPushMsg;
import com.wuliu.system.service.JPushClientService;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.SysPushMsgMapper;
import com.wuliu.system.service.ISysPushMsgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * app推送消息Service业务层处理
 *
 * @author realue
 * @date 2023-11-06
 */
@Service
public class SysPushMsgServiceImpl extends ServiceImpl<SysPushMsgMapper, SysPushMsg> implements ISysPushMsgService {
    @Resource
    private SysPushMsgMapper sysPushMsgMapper;

    /**
     * 查询app推送消息
     *
     * @param id app推送消息主键
     * @return app推送消息
     */
    @Override
    public SysPushMsg selectSysPushMsgById(String id) {
        return sysPushMsgMapper.selectSysPushMsgById(id);
    }

    /**
     * 查询app推送消息列表
     *
     * @param sysPushMsg app推送消息
     * @return app推送消息
     */
    @Override
    public List<SysPushMsg> selectSysPushMsgList(SysPushMsg sysPushMsg) {
        return sysPushMsgMapper.selectSysPushMsgList(sysPushMsg);
    }

    /**
     * 新增app推送消息
     *
     * @param sysPushMsg app推送消息
     * @return 结果
     */
    @Override
    public int insertSysPushMsg(SysPushMsg sysPushMsg) {
        sysPushMsg.setCreateTime(DateUtils.getNowDate());
        return sysPushMsgMapper.insertSysPushMsg(sysPushMsg);
    }

    /**
     * 修改app推送消息
     *
     * @param sysPushMsg app推送消息
     * @return 结果
     */
    @Override
    public int updateSysPushMsg(SysPushMsg sysPushMsg) {
        sysPushMsg.setUpdateTime(DateUtils.getNowDate());
        return sysPushMsgMapper.updateSysPushMsg(sysPushMsg);
    }

    /**
     * 批量删除app推送消息
     *
     * @param ids 需要删除的app推送消息主键
     * @return 结果
     */
    @Override
    public int deleteSysPushMsgByIds(String[] ids) {
        return sysPushMsgMapper.deleteSysPushMsgByIds(ids);
    }

    /**
     * 删除app推送消息信息
     *
     * @param id app推送消息主键
     * @return 结果
     */
    @Override
    public int deleteSysPushMsgById(String id) {
        return sysPushMsgMapper.deleteSysPushMsgById(id);
    }

    /**
     * 推送app消息
     *
     * @param sysAccount
     * @param pushMsgs
     */
    @Override
    public void sysPushMsgs(String sysAccount, List<SysPushMsg> pushMsgs) {
        List<SysPushMsg> updatePushMsg = new ArrayList<>();
        JPushClientService jPushClientService = new JPushClientService(sysAccount);
        pushMsgs.forEach(t -> {
            SysPushMsg sysPushMsg = new SysPushMsg();
            sysPushMsg.setId(t.getId());
            sysPushMsg.setUpdateTime(DateUtils.getNowDate());
            sysPushMsg.setPushTime(DateUtils.getTime());
            if (jPushClientService.sendToAlias(Collections.singletonList(t.getAlias()), t.getNotificationTitle(), t.getMsgTitle(), t.getContent(), t.getJumpUrl(), t.getId())) {
                sysPushMsg.setPushFlag(1);
            } else {
                sysPushMsg.setPushFlag(2);
            }
            updatePushMsg.add(sysPushMsg);
        });
        this.updateBatchById(updatePushMsg);
    }

    /**
     * 新增app消息通知
     *
     * @param msgType    消息类型 1.新订单 2.超时揽收
     * @param alias      别名
     * @param sysAccount 账套号
     */
    @Override
    public void createPushMsg(Integer msgType, String alias, String sysAccount) {
        SysPushMsg sysPushMsg = new SysPushMsg();
        sysPushMsg.setId(IdUtil.randomUUID());
        sysPushMsg.setSysAccount(sysAccount);
        sysPushMsg.setCreateTime(DateUtils.getNowDate());
        sysPushMsg.setAlias(alias);
        sysPushMsg.setMsgType(msgType);
        //TODO 根据类型设置标题、内容
        String notificationTitle = "";
        String msgTitle = "";
        String msgContent = "";
        if (msgType == 1) {
            notificationTitle = "您有新订单，请及时处理！";
            msgTitle = "";
            msgContent = DictUtils.getSysConfigValue(sysAccount, "new_order_reminder_tone");
        }
        sysPushMsg.setNotificationTitle(notificationTitle);
        sysPushMsg.setMsgTitle(msgTitle);
        sysPushMsg.setContent(msgContent);
        sysPushMsg.setJumpUrl("");
        sysPushMsgMapper.insert(sysPushMsg);
    }
}
